package com.auto.base.datastore

import android.content.Context
import androidx.datastore.core.DataStore
import androidx.datastore.preferences.core.PreferenceDataStoreFactory
import androidx.datastore.preferences.core.Preferences
import androidx.datastore.preferences.core.booleanPreferencesKey
import androidx.datastore.preferences.core.doublePreferencesKey
import androidx.datastore.preferences.core.edit
import androidx.datastore.preferences.core.floatPreferencesKey
import androidx.datastore.preferences.core.intPreferencesKey
import androidx.datastore.preferences.core.longPreferencesKey
import androidx.datastore.preferences.core.stringPreferencesKey
import androidx.datastore.preferences.preferencesDataStoreFile
import dagger.Module
import dagger.Provides
import dagger.hilt.InstallIn
import dagger.hilt.android.qualifiers.ApplicationContext
import dagger.hilt.components.SingletonComponent
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.map
import javax.inject.Inject
import javax.inject.Singleton

/**
 * Created by YuanWei1 on 2025/6/9.
 *
 * Description:
 */


@Singleton
class DataStoreUtil @Inject constructor(private val dataStore: DataStore<Preferences>) {
    // 使用伴生对象存放所有Key常量
    object Key {

        // nas的ip地址
        const val NAS_IP = "NAS_IP"
        const val QL = "QL"
        const val TOKEN = "TOKEN"
        const val NAME = "NAME"
        const val PWD = "PWD"
        const val IP = "IP"
    }

    // 基础类型存储
    suspend fun <T> saveData(key: String, value: T) {
        when (value) {
            is Int -> dataStore.edit { it[intPreferencesKey(key)] = value }
            is String -> dataStore.edit { it[stringPreferencesKey(key)] = value }
            is Boolean -> dataStore.edit { it[booleanPreferencesKey(key)] = value }
            is Float -> dataStore.edit { it[floatPreferencesKey(key)] = value }
            is Long -> dataStore.edit { it[longPreferencesKey(key)] = value }
            is Double -> dataStore.edit { it[doublePreferencesKey(key)] = value }
        }
    }

    // 基础类型读取
    fun <T> getData(key: String, defaultValue: T): Flow<T> {
        return when (defaultValue) {
            is Int -> dataStore.data.map { it[intPreferencesKey(key)] ?: defaultValue }
            is String -> dataStore.data.map { it[stringPreferencesKey(key)] ?: defaultValue }
            is Boolean -> dataStore.data.map { it[booleanPreferencesKey(key)] ?: defaultValue }
            is Float -> dataStore.data.map { it[floatPreferencesKey(key)] ?: defaultValue }
            is Long -> dataStore.data.map { it[longPreferencesKey(key)] ?: defaultValue }
            is Double -> dataStore.data.map { it[doublePreferencesKey(key)] ?: defaultValue }
            else -> throw IllegalArgumentException("Unsupported type")
        } as Flow<T>
    }


    // 通用删除方法（修正版）
    suspend fun removeData(key: String) {
        dataStore.edit { preferences ->
            when {
                preferences.contains(stringPreferencesKey(key)) ->
                    preferences.remove(stringPreferencesKey(key))

                preferences.contains(intPreferencesKey(key)) ->
                    preferences.remove(intPreferencesKey(key))

                preferences.contains(booleanPreferencesKey(key)) ->
                    preferences.remove(booleanPreferencesKey(key))

                preferences.contains(floatPreferencesKey(key)) ->
                    preferences.remove(floatPreferencesKey(key))

                preferences.contains(longPreferencesKey(key)) ->
                    preferences.remove(longPreferencesKey(key))

                preferences.contains(doublePreferencesKey(key)) ->
                    preferences.remove(doublePreferencesKey(key))
            }
        }
    }

    // 清空所有数据
    suspend fun clearAll() {
        dataStore.edit { it.clear() }
    }
}

@Module
@InstallIn(SingletonComponent::class)
object DataStoreModule {
    private const val DATASTORE_NAME = "NasToolBox"

    @Provides
    @Singleton
    fun providePreferencesDataStore(
        @ApplicationContext context: Context
    ): DataStore<Preferences> {
        return PreferenceDataStoreFactory.create(
            produceFile = { context.preferencesDataStoreFile(DATASTORE_NAME) }
        )
    }
}